// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

// import GameManager from "./gameManager";
import { BaseFullComponent } from "./base/BaseFullComponent";
import Utils from "./Utils";

const { ccclass, property } = cc._decorator;

/**
 * 摄像机控制类
 */
@ccclass
export default class CameraController extends BaseFullComponent {
  public static instance: CameraController = null;

  @property(cc.Camera)
  camera: cc.Camera = null;

  private target: cc.Node = null;
  private targetPos: cc.Vec3 = cc.Vec3.ZERO;

  private _mapParams: cc.Node = null;

  // LIFE-CYCLE CALLBACKS:

  onLoad() {
    CameraController.instance = this;
  }
  protected start(): void {
    super.start();
    this.resize();
  }

  public init(mapParams: cc.Node): void {
    this._mapParams = mapParams;
  }

  update(dt) {
    this.followTarget(dt);
  }

  /**
   * 获得摄像机坐标
   * @returns
   */
  public getCameraPos(): cc.Vec3 {
    if (this.camera == null) {
      return cc.Vec3.ZERO;
    }

    //对摄像机位置向上取整后返回
    return new cc.Vec3(
      Math.ceil(this.camera.node.position.x),
      Math.ceil(this.camera.node.position.y),
      0
    );
  }

  public setTarget(target: cc.Node) {
    this.target = target;
  }

  /**
   * 视野跟随目标
   * @param dt
   */
  public followTarget(dt: number): void {
    if (this.target == null || !this.target.isValid || this.camera == null) {
      return;
    }
    // this.targetPos = this.target.position.sub(cc.v3(cc.winSize.width / 2, cc.winSize.height / 2));

    // if (this._mapParams.width < cc.winSize.width) {
    //     this.targetPos.x = (this._mapParams.width - cc.winSize.width) / 2;
    // } else {
    //     if (this.targetPos.x > this._mapParams.width - cc.winSize.width) {
    //         this.targetPos.x = this._mapParams.width - cc.winSize.width;
    //     } else if (this.targetPos.x < 0) {
    //         this.targetPos.x = 0;

    //     }
    // }

    // if (this._mapParams.height < cc.winSize.height) {
    //     this.targetPos.y = (this._mapParams.height - cc.winSize.height) / 2;
    // } else {
    //     if (this.targetPos.y > this._mapParams.height - cc.winSize.height) {
    //         this.targetPos.y = this._mapParams.height - cc.winSize.height;
    //     } else if (this.targetPos.y < 0) {
    //         this.targetPos.y = 0;
    //     }
    // }

    // this.targetPos = Utils.getLocalPositionWithOtherNode(this.node, this.target)
    //摄像机平滑跟随
    this.targetPos = Utils.getLocalPositionWithOtherNode(
      this.node.parent,
      this.target
    );
    this.camera.node.position.lerp(this.targetPos, dt * 3, this.targetPos);
    this.camera.node.position = this.targetPos;
  }

  /**
   *把视野定位到给定位置
   * @param px
   * @param py
   *
   */
  public setViewToPoint(px: number, py: number): void {
    // this.targetPos = Utils.getLocalPositionWithOtherNode(this.node, this.target)
    // this.targetPos = cc.v3(px, py).sub(cc.v3(cc.winSize.width / 2, cc.winSize.height / 2));

    // if (this._mapParams.width < cc.winSize.width) {
    //     this.targetPos.x = (this._mapParams.width - cc.winSize.width) / 2;
    // } else {
    //     if (this.targetPos.x > this._mapParams.width - cc.winSize.width) {
    //         this.targetPos.x = this._mapParams.width - cc.winSize.width;
    //     } else if (this.targetPos.x < 0) {
    //         this.targetPos.x = 0;

    //     }
    // }

    // if (this._mapParams.height < cc.winSize.height) {
    //     this.targetPos.y = (this._mapParams.height - cc.winSize.height) / 2;
    // } else {
    //     if (this.targetPos.y > this._mapParams.height - cc.winSize.height) {
    //         this.targetPos.y = this._mapParams.height - cc.winSize.height;
    //     } else if (this.targetPos.y < 0) {
    //         this.targetPos.y = 0;
    //     }
    // }
    this.targetPos = Utils.getLocalPositionWithOtherNode(
      this.node.parent,
      this.target
    );

    this.camera.node.position = this.targetPos;
  }

  resize() {
    super.resize();
    cc.director
      .getScene()
      .getComponentsInChildren(cc.Widget)
      .forEach(function (t) {
        t.updateAlignment();
      });

    /**适配 */
    let _sh = cc.winSize.height;
    let _sw = cc.winSize.width;

    this.camera.zoomRatio = 0.8;
    let ratio: number = this.getCanvasRatio();
    this.camera.zoomRatio = 1;
    if (cc.winSize.height > cc.winSize.width) {
      //竖屏

      this.camera.zoomRatio = 0.8;
    } else {
      this.camera.zoomRatio = 2;
      if (ratio >= 0.68) {
        //ipad
        this.camera.zoomRatio = 1.4;
      } else if (ratio > 0.66) {
        //iphone4
      } else if (ratio > 0.59) {
        //短屏幕<1280)
      } else if (ratio < 0.48) {
        //iphonex
      } else {
      }
    }
    this.camera.zoomRatio += 0.4;
    // GameManager.uiCam.zoomRatio = CameraController.instance.camera.zoomRatio;
  }

  /**获取屏幕比例 */
  public getCanvasRatio() {
    let ratioX: number = cc.winSize.width / cc.winSize.height;
    let ratioY: number = cc.winSize.height / cc.winSize.width;
    if (cc.winSize.height > cc.winSize.width) {
      //竖屏
      return ratioX;
    } else {
      return ratioY;
    }
  }
  protected onDestroy(): void {
    CameraController.instance = null;
  }
}
